define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ad/ad/index',
                    add_url: 'ad/ad/add',
                    edit_url: 'ad/ad/edit',
                    del_url: 'ad/ad/del',
                    multi_url: 'ad/ad/multi',
                    table: 'ad',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh desc, id desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'adposition.position_name', title: __('Position.position_name'), formatter: Table.api.formatter.search},
                        {field: 'ad_name', title: __('Ad_name'), formatter: Table.api.formatter.search},
                        {field: 'ad_file', title: __('Ad_file'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'format', title: __('Format'), searchList: {"0":__('Format 0'),"1":__('Format 1')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Controller.api.formatter.status},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'link_man', title: __('Link_man'), formatter: Table.api.formatter.search},
                        {field: 'admin.nickname', title: __('Admin.nickname'), formatter: Table.api.formatter.search},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showColumns: false,
                showToggle: false,
                showExport: false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                var input_id = $('#plupload-ad_file').data("input-id") ? $('#plupload-ad_file').data("input-id") : "";
                var preview_id = $('#plupload-ad_file').data("preview-id") ? $('#plupload-ad_file').data("preview-id") : "";
                if (preview_id) {
                    // 移除按钮事件
                    $(document.body).off("click", "#" + preview_id + " .btn-trash");
                    $(document.body).on("click", "#" + preview_id + " .btn-trash", function () {
                        var that = $(this);
                        var path = $("#" + preview_id + " [data-url]");
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'ad/ad/delUpload',
                                    data: {
                                        adid: $("#plupload-ad_file").data('ad-id'),
                                        path: path.data("url")
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    if (input_id) {
                                        $("#" + input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-ad_file").show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });
                }
            },
            formatter: {
                status: function (value, row, index) {
                    return '<a class="btn-change text-success" data-url="'+$.fn.bootstrapTable.defaults.extend.multi_url+'" data-id="' + row.id + '" data-params="status=' + (row.status == '0' ? '1' : '0') + '"><i class="fa ' + (row.status == '0' ? 'fa-toggle-off' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                }
            }
        }
    };
    return Controller;
});
